/*****  Component, by Ben Bi <prince.bi@gmail.com>  created at: 12/23/2009 4:10:45 PM  *****/

/********   Notes:  *******
 *  Two tags need to be replaced with the module name, they are:
 *  wacGuardGroup
 *  WacGuardGroup
 *  wac_guard_group
 */



/***** variables declartion section, begin *****/
var wacGuardGroupInputMode = wacFormInputMode.add;
var wacGuardGroupObj = {};

/***** variables declartion section, end *****/

/***** init section, begin *****/
$(document).ready(
    function() {
        initWacGuardGroupFormDialog();

        initWacGuardGroupForm();

        bindWacGuardGroupEvents();
    }
);

function bindWacGuardGroupEvents()
{
    $("#wacGuardGroupFormDialog").bind('dialogopen', function(event, ui)
    {
        initWacGuardGroupFormData();
//        wacDebugLog("calling " + moduleName + ":" + wacModelObj.wacGuardGroup.name);//
    });

    $("#wacGuardGroupFormDialog").bind('dialogclose', function(event, ui)
    {
        $.validationEngine.closePrompt(".formError", true);
    });

    $("#wacGuardGroup_btnSubmit").bind('click', function (e)
    {
        submitWacGuardGroupForm();
    });

    $("#wacGuardGroup_btnClose").bind('click', function (e)
    {
        $("#wacGuardGroupFormDialog").dialog('close');
    });

    // fix dialog div didnt remove bug, remove it by this way
    $('#appStorehouseRightPane').bind('tabsremove', function(event, ui) {
        if(ui.panel.id == "t2302")
        {
            $("div[aria-labelledby='ui-dialog-title-wacGuardGroupFormDialog']").remove();
            $("#wacGuardGroupFormDialog").remove();
        }
    });
}


function initWacGuardGroupFormDialog()
{
//    wacDebugLog("initWacGuardGroupFormDialog.");
    $("#wacGuardGroupFormDialog").dialog({
            bgiframe: true,
            modal: true,
            width: 850,
            height: 480,
            autoOpen: false,
            buttons: {},
            zIndex: 100
        });
}

function initWacGuardGroupForm()
{
    $("#wacGuardGroupForm").validationEngine();

    $('.wacFormContent .left, .wacFormContent input, .wacFormContent textarea, .wacFormContent select').focus(function(){
        $(this).parents('.wacFormContent').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormContent').removeClass("wacFormOver");
    });
    
    $('.wacFormContentA .left, .wacFormContentA input, .wacFormContentA textarea, .wacFormContentA select').focus(function(){
        $(this).parents('.wacFormRow').addClass("wacFormOver");
    }).blur(function(){
        $(this).parents('.wacFormRow').removeClass("wacFormOver");
    });

    
}

function initWacGuardGroupFormData()
{
    wacShowBlockUILoading("#wacGuardGroupFormDialog");
    $('#wacGuardGroup_sf_guard_group_permissions_list').empty();

    if(wacGuardGroupInputMode == wacFormInputMode.add)   // use default values
    {
        var params = {
            "data_format" :'json'
        };
    }
    else
    {
        var params = {
            "data_format" :'json',
            "id" : wacGuardGroupObj.id
        };
    }
    

    $.ajax({
        url: BASE_URL + "wacGuardGroup/getFormData",
        global: true,
        type: "GET",
        data: params,
        dataType: "json",
        success: function(jsonData){
            initWacGuardGroupFormDataCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function initWacGuardGroupFormDataCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       alert(jsonData.info.error_info);
   }
   else
    {
        for(i=0;i<jsonData['items']['permission'].length;i++)
        {
            $('<option value="' + jsonData['items']['permission'][i].key +'">' + jsonData['items']['permission'][i].value +'</option>').appendTo("#wacGuardGroup_sf_guard_group_permissions_list");
        }

        for(i=0;i<jsonData['items']['group_permission'].length;i++)
        {
            $("#wacGuardGroup_sf_guard_group_permissions_list option[value='"+jsonData['items']['group_permission'][i]+"']").attr("selected", true);
//            $("#wacGuardGroup_sf_guard_group_permissions_list option[value=2]").attr("selected", "selected");
        }
   }

   setupWacGuardGroupFormDefaultValues(jsonData['items']['default']);

   wacHideBlockUI("#wacGuardGroupFormDialog");

//   wacDebugLog($.dump(jsonData));
}

function setupWacGuardGroupFormDefaultValues(defaultValueObj)
{
    if(wacGuardGroupInputMode == wacFormInputMode.add)   // use default values
    {
        $("#wacGuardGroup_id").attr("value", 0);
    }
    else  // use edit obj values
    {
        $("#wacGuardGroup_id").attr("value", wacGuardGroupObj.id);
        $("#wacGuardGroup_name").attr("value", wacGuardGroupObj.name);
        $("#wacGuardGroup_description").attr("value", wacGuardGroupObj.description);
    }

//    wacDebugLog($.dump(wacGuardGroupObj));
//    wacDebugLog($.dump(defaultValueObj));
//    wacDebugLog("wacGuardGroupInputMode: " + wacGuardGroupInputMode);

}
/***** init section, end *****/


/**************  interaction section, begin  ***************/

function wacGuardGroupOpenModuleForm(moduleFormName, moduleName, inputMode, rowid)
{
    wacGuardGroupInputMode = inputMode;

    if(rowid === undefined){
        wacGuardGroupObj = {};
    }
    else
    {
        wacGuardGroupObj = $("#" + moduleName +"List").getRowData(rowid);
    }

    $('#'+moduleFormName).dialog('open');
}

function validateWacGuardGroupForm()
{
    var validateFlag = true;
    
    if (!$("#wacGuardGroupForm").validationEngine({returnIsValid:true}))
    {
        validateFlag = false;
        return validateFlag;
    }

    if($("#wacGuardGroup_sf_guard_group_permissions_list :selected").length == 0)
    {
        wacShowTips("请选择至少一个权限!");
        validateFlag = false;
    }
    
    return validateFlag;
}

function submitWacGuardGroupForm()
{
    if(!validateWacGuardGroupForm()){
        return;
    }

    wacShowBlockUILoading("#wacGuardGroupFormDialog", "处理中...");

    var extraParams = "data_format=json";
    var submitUrl;

    if(wacGuardGroupInputMode == wacFormInputMode.add){
        submitUrl = BASE_URL + "wacGuardGroup/add";
    }
    else{
        submitUrl = BASE_URL + "wacGuardGroup/edit";
    }


    $.ajax({
        url: submitUrl,
//        url: BASE_URL + "test/ajaxTest" ,
        global: true,
        type: "GET",
        data: $("#wacGuardGroupForm").serialize() + "&" + extraParams,
        dataType: "json",
        success: function(jsonData){
            submitWacGuardGroupFormCallBack(jsonData);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            wacDebugLog("getFormData Error: " + $(this).dump()); // the options for this ajax request
        }
    });
}

function submitWacGuardGroupFormCallBack(jsonData)
{
   if(jsonData.info.status == wacOperationStatus.Error)
   {
       wacShowTips(jsonData.info.error_info);
   }
   else
   {
       wacShowTips(jsonData.info.tips);
       $("#wacGuardGroupList").trigger("reloadGrid");
   }

   wacHideBlockUI("#wacGuardGroupFormDialog");

//   wacDebugLog($.dump(jsonData));
}

/**************  interaction section, end  ***************/